#include <iostream>
#include <unistd.h>
#include <pthread.h>
#include "RingQueue.hpp"

void *Productor(void *args)
{
    sleep(5);
    RingQueue<int> *rq = static_cast<RingQueue<int> *>(args);
    int data = 0;
    while (true)
    {
        std::cout << "生产数据... data : " << data << std::endl;
        rq->Push(data);
        data += 1;
        sleep(1);
    }
    return nullptr;
}

void *Consumer(void *args)
{
    RingQueue<int> *rq = static_cast<RingQueue<int> *>(args);
    int data = 0;
    while (true)
    {
        rq->Pop(&data);
        std::cout << "消费数据... data : " << data << std::endl;
        sleep(1);
    }
    return nullptr;
}
int main()
{
    RingQueue<int> *rq = new RingQueue<int>();
    // 创建线程
    pthread_t p[3], c[2];

    pthread_create(p, nullptr, Productor, rq);
    pthread_create(p+1, nullptr, Productor, rq);
    pthread_create(p+2, nullptr, Productor, rq);
    pthread_create(c, nullptr, Consumer, rq);
    pthread_create(c+1, nullptr, Consumer, rq);

    // 等待线程
    pthread_join(p[0], nullptr);
    pthread_join(p[1], nullptr);
    pthread_join(p[2], nullptr);
    pthread_join(c[1], nullptr);
    pthread_join(c[2], nullptr);
}